<%
' +----------------------------------------------------------------------
' | POPASP [ ASP MVC ]
' +----------------------------------------------------------------------
' | Copyright (c) 2016 http://popasp.com All rights reserved.
' +----------------------------------------------------------------------
' | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
' +----------------------------------------------------------------------
' | Author: popasp <1737025626@qq.com>
' +----------------------------------------------------------------------
Class POPASP_PAGE
	private rs	
	private page	
	private url
	private matches
	private parameter
	
	'下面是可配置的
	private header
	private first
	private prev
	private [next]
	private last
	private noRecordTip
	private pageCount
	private seperator
	private pageScale

	Private Sub Class_Initialize
		dim arr,pos
		header = "条记录"
		first = "首页"
		prev = "上一页"
		[next] = "下一页"
		last = "尾页"
		pageCount = 5
		noRecordTip = "没有可载入的数据"
		seperator = "&nbsp;"
		pageScale = true
		
		url = C_("TMPL_PARSE_STRING.__SELF__")
		if Request.QueryString = "" then
			pos = InStr( C_("TMPL_PARSE_STRING.__URL__"),"?" )
			if pos < 0  Then
				url = C_("TMPL_PARSE_STRING.__URL__") & "?" &  C_("VAR_PAGE") & "=1"				
			Elseif pos = Len( C_("TMPL_PARSE_STRING.__URL__") ) Then
				url = C_("TMPL_PARSE_STRING.__URL__") &  C_("VAR_PAGE") & "=1"
			Else
				url = C_("TMPL_PARSE_STRING.__URL__") & "&" &  C_("VAR_PAGE") & "=1"	
			End if
			page = 1
		elseif not isEmpty( Request.QueryString( C_("VAR_PAGE") ) ) then
			page =  Request.QueryString( C_("VAR_PAGE") )
		else
			url = url & "&" & C_("VAR_PAGE") & "=1"
			page = 1
		end if
		if page < 1 then
			page = 1
		end if
 	End Sub
	
	Private Sub Class_Terminate
		set matches = nothing
	End Sub
	
	Public Function getConfig(attr)
		Execute "getConfig = " & attr
	end Function
	
	Public Sub setConfig(attr,value)
		if isNumeric(value) then
			Execute attr & " = " & value
		else
			Execute attr & " = """ & value & """"
		end if
	End Sub
	
	Public Default Function Constructor(byref args)
		if isArray(args) then
			set rs = args(0)
			if ubound( args ) > 0 then
				parameter = args(1)
			end if
		else
			set rs = args
			parameter = ""
		end if
		set Constructor = Me
		page = CInt(page)
		if page > rs.PageCount then
			page = rs.PageCount
		end if		
	End Function
	
	Public Function getUrl(dPage,sTxt)
		dim l_para,find_str
		set matches = POP_MVC.String.reg_exec(url,"[\?&](" & C_("VAR_PAGE") & "=\d+)","i")
		find_str = matches(0).subMatches(0)
		
		getUrl = replace( url, find_str , C_("VAR_PAGE") & "=" & dPage )
		if parameter <> "" AND inStr( POP_MVC.urldecode(getUrl),POP_MVC.urldecode(parameter) ) < 1 then		
			getUrl = getUrl & "&" & parameter
		end if
		getUrl = "<a href='" & getUrl & "'>" & sTxt & "</a>"
	End Function
	
	Public Function Show()
		dim dLeft,dRight,nav,sFirst,sPrev,sNext,sLast
		nav = array()
		if rs.RecordCount < 1 Then
			Show = noRecordTip
			Exit Function
		end if
		if rs.PageCount <= 1 OR page > rs.pageCount Then
			Show = ""
			Exit Function
		End if
		
		POP_MVC.Arr.push nav,"<em>" & page & "</em>"
		
		dLeft = page - 1
		dRight = page + 1
		
		do While dLeft >= 1 OR dRight <= rs.PageCount
			
			if UBound(nav) >= pageCount then
				exit do
			end if
			
			
			
			if dLeft>= 1 then	
				POP_MVC.Arr.Unshift nav,getUrl(dLeft,dLeft)				
				dLeft = dLeft - 1
			end if
			
			if dRight <= rs.PageCount then				
				POP_MVC.Arr.Push nav,getUrl(dRight,dRight)
				dRight = dRight + 1
			end if
		Loop
		
		
		
		if page = 1 then 
			sFirst = ""
			sPrev = ""
		else
			if not is_empty(prev) 	then sPrev = getUrl(page-1,prev) : POP_MVC.Arr.Unshift nav,sPrev
			if not is_empty(first) 	then sFirst = getUrl(1,first) : POP_MVC.Arr.Unshift nav,sFirst						
		end if
		
		if page = rs.PageCount then
			sLast = ""
			sNext = ""
		else
			if not is_empty([next]) then 	sNext = getUrl( page+1,[next] ) : POP_MVC.Arr.Push nav,sNext
			if not is_empty(last) 	then 	sLast = getUrl( rs.PageCount,last ) : POP_MVC.Arr.Push nav,sLast				
		end if		
		if not is_empty(pageScale) then POP_MVC.Arr.Unshift nav,page & "/" & rs.PageCount & "页"
		if not is_empty(header) then POP_MVC.Arr.Unshift nav,"共" & rs.RecordCount & header
		Show = join( nav,seperator )
	End Function
	
End Class
%>